#include <iostream>
#include <chrono>
#include <random>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <functional>
#define VNAME(name) (#name)
#define debug(x) {int debugx = x; std::cout << " (" << VNAME(x) << ": " << (debugx) << ")\n";}
#define F_(n) cout << fixed << setprecision(n)
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
bool _linux_Flag;
/*static int _ = [](){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    return 0;
}();//*/
typedef pair<int, int> PII;
int solve(int n) {
    int num, minn, q_ = 0, v_ = 0;
    vector<PII> v(1, {minn = 0x3f3f3f3f, 0}), q(1, {0, 0});
    for (int i = 1; i <= n; ++ i) {
        cin >> num;
        if (v.back().first > num) v.push_back({num, i}), v_ ++;
    }
    for (int i = 1; i <= n; ++ i) {
        cin >> num;
        if (q.back().first < num) q.push_back({num, i}), q_ ++;
    }
    for (int i = 1; i <= v_ ; ++ i) {
        while (v[i].first < q[q_ - 1].first) q_ --;
        //cout << i << ' ' << q_ << ' ';
        minn = min(minn, q[q_].second + v[i].second);
    }
    return minn - 2;
}
signed main() {
    #ifndef ONLINE_JUDGE
		_linux_Flag = false; //freopen("C:\\Users\\lenovo\\Desktop\\in.in", "r", stdin);
	#endif
    int t = 1;
    cin >> t;
    while (t --) {
        int n;
        cin >> n;
        //solve();
        cout << solve(n) << '\n';
        //debug(solve(n));
    }
    #ifndef ONLINE_JUDGE
		system("pause");
	#endif
    return 0;
}